﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Docking = WeifenLuo.WinFormsUI.Docking;
using System.Drawing;

namespace EdytorWF.Objects
{
    enum EModes
    {
        CREATE_OBJECT = 0,
        SELECT = 1,
    };

    class IEditor
    {
        ObjectsDlg ObjectsDialog;
        TypesObjectDlg TypesDialog;
        private static IEditor I_Editor;
        public EModes ActiveMode { get; set; }
        public String SelectedObjectName = "No object selected";

        private IEditor()
        {
            ActiveMode = EModes.SELECT;
        }

        //////////////////////////////////////////////////////////////////////////
        public static IEditor EditorInstance
        {
            get
            {
                if (I_Editor == null)
                {
                    I_Editor = new IEditor();
                }
                return I_Editor;
            }
        }

        ////////////////////////////////////////////////////////////////////////
        public void OnLeftButtonClick(Point click)
        {
            if (ActiveMode == EModes.SELECT)
            {
                IObject obj = IEngine.EngineInstance.FindObjectByCollisionNew(click);
                if (obj != null)
                {
                    ObjectsDialog.SelectObject(obj);
                    IObject sel_obj = ObjectsDialog.GetSelectedObject();
                    if (sel_obj != null)
                    {
                        SelectedObjectName = ((ModelObject)sel_obj).ObjectName;
                    }
                }
                else
                {
                    SelectedObjectName = "No object selected";
                }
            }
            else if (ActiveMode == EModes.CREATE_OBJECT)
            {
                if (IEngine.EngineInstance.CreateObjectMode == ECreateObjectModes.COLLISION)
                {
                    if (IEngine.EngineInstance.FindCollisionOnTerrain(click))
                    {
                        IEditor.EditorInstance.PutObject();
                    }
                }
                else 
                {
                    IEditor.EditorInstance.PutObject();
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////
        public void PutObject()
        {
            if (TypesDialog.ObjectTypeName == "_MODEL_OBJECT")
            {
                CreateModelObject();
            }
            else if (TypesDialog.ObjectTypeName == "_SOUND_OBJECT")
            {
                CreateSoundObject();
            }
            else if (TypesDialog.ObjectTypeName == "_LIGHT_OBJECT")
            {
                CreateLightObject();
            }
            else if (TypesDialog.ObjectTypeName == "_PARTICLE_OBJECT")
            {
                CreateParticleObject();
            }
        }

        ////////////////////////////////////////////////////////////////////////
        private void CreateParticleObject()
        {
            bool ret = IEngine.EngineInstance.CreateParticleObject();
            if (ret)
            {
                ObjectsDialog.RefreshObjectList();
            }
        }

        ////////////////////////////////////////////////////////////////////////
        public void CreateModelObject()
        {
            bool ret = IEngine.EngineInstance.CreateModelObject();
            if (ret)
            {
                ObjectsDialog.RefreshObjectList();
            }
        }

        //////////////////////////////////////////////////////////////////////
        public void CreateSoundObject()
        {
            bool ret = IEngine.EngineInstance.CreateSoundObject();
            if (ret)
            {
                ObjectsDialog.RefreshObjectList();
            }
        }

        //////////////////////////////////////////////////////////////////////
        public void CreateLightObject()
        {
            bool ret = IEngine.EngineInstance.CreateLightObject();
            if (ret)
            {
                ObjectsDialog.RefreshObjectList();
            }
        }

        //////////////////////////////////////////////////////////////////////
        public void ShowObjectDlg(Docking.DockPanel dockPanel)
        {
            ObjectsDialog = new ObjectsDlg();
            ObjectsDialog.Show(dockPanel);
        }
        /////////////////////////////////////////////////////////////////////
        public void ShowTypesDlg(Docking.DockPanel dockPanel)
        {
            TypesDialog = new TypesObjectDlg();
            TypesDialog.Show(dockPanel);
        }
        /////////////////////////////////////////////////////////////////////
        public void SetPlaceObjectInCamera()
        {
            IEngine.EngineInstance.SetCreateObjectMode(ECreateObjectModes.CAMERA);
        }
        /////////////////////////////////////////////////////////////////////
        public void SetPutObjectMode()
        {
            IEngine.EngineInstance.SetCreateObjectMode(ECreateObjectModes.COLLISION);
        }
        /////////////////////////////////////////////////////////////////////
        public void Refresh()
        {
            IEngine.EngineInstance.Refresh();
        }
        /////////////////////////////////////////////////////////////////////
    }
}
